package blog.open1111.controller;

import blog.open1111.entity.Article;
import blog.open1111.entity.PageBean;
import blog.open1111.service.ArticleService;
import blog.open1111.util.PageUtil;
import blog.open1111.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dengwubo on 2017/9/14.
 */
@Controller
@RequestMapping("/")
@SuppressWarnings("all")
public class IndexController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/index")
    public ModelAndView index(@RequestParam(value="page",required=false,defaultValue = "1")String page, @RequestParam(value="typeId",required=false)String typeId, HttpServletRequest request)throws Exception{
        ModelAndView mav=new ModelAndView();
        PageBean pageBean = new PageBean(Integer.parseInt(page),10);
        Map<String,Object>map = new HashMap<>();
        map.put("start",pageBean.getStart());
        map.put("size",pageBean.getPageSize());
        map.put("typeId",typeId);
        List<Article> articleList =articleService.list2(map);
        Long total = articleService.getTotal2(map);
        mav.addObject("pageTitle","首页");
        mav.addObject("magePage","foreground/article/list.jsp");
        mav.addObject("articleList",articleList);
        StringBuilder param = new StringBuilder();
        if (StringUtil.isNotEmpty(typeId)){
            param.append("typeId="+typeId+"&");
        }
        mav.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"index.html",total,Integer.parseInt(page),10,param.toString()));

        mav.setViewName("mainTemp");
        return mav;
    }

    @RequestMapping("/aboutMe")
    public ModelAndView aboutMe() throws Exception{
        ModelAndView mav = new ModelAndView();
        mav.addObject("pageTitle","关于我们");
        mav.addObject("magePage","foreground/system/aboutMe.jsp");
        mav.setViewName("mainTemp");
        return mav;

    }

}
